<?php
App::uses('AppController', 'Controller');
/**
 * Movies Controller
 *
 * @property Movie $Movie
 */
class MoviesController extends AppController {
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('index');
	}
	
	public function beforeRender(){
		parent::beforeRender();
		switch($this->action){
			case 'index':
				$this->layout = 'default';
				break;
			default:
				$this->layout = 'panel';
		}
	}

/**
 * index method
 *
 * @return void
 */
	public function admin() {
		$this->Movie->recursive = 0;
		$options['conditions'] = array('Movie.domain_id'=> $this->Session->read('Config.Domain.Domain.id'));
		$this->paginate = $options;
		$this->set('movies', $this->paginate());
	}
	
	public function index() {
		$this->Movie->recursive = 0;
		$options['conditions'] = array('Movie.domain_id'=> $this->Session->read('Config.Domain.Domain.id'));
		$this->paginate = $options;
		$this->set('movies', $this->paginate());
		
		$this->loadModel('Gallery');
		$this->Gallery->recursive = -1;
		
		// najnowsze
		$options['joins'] = array(
		array('table' => 'galleries_uploads',
						        'alias' => 'GalleriesUpload',
						        'type' => 'left',
						        'conditions' => array('Gallery.id = GalleriesUpload.gallery_id'),
		
		),
		array('table' => 'uploads',
						        'alias' => 'Upload',
						        'type' => 'left',
						        'conditions' => array('GalleriesUpload.upload_id = Upload.id')
		)
		);
		$options['fields'] = array('Gallery.id','Gallery.name','Gallery.date','Gallery.slug','Gallery.description','Upload.name');
		
		$options['conditions'] = array('Gallery.domain_id' => $this->Session->read('Config.Domain.Domain.id'));
		$options['limit'] = 6;
		$options['order'] = array('Gallery.date DESC');
		$options['group'] = array('Gallery.id');
		$this->set('galleries', $this->Gallery->find('all', $options));
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Movie->id = $id;
		if (!$this->Movie->exists()) {
			throw new NotFoundException(__('Invalid movie'));
		}
		$this->set('movie', $this->Movie->read(null, $id));
	}
	
	
	public function get($limit) {
		$this->Movie->recursive = -1;
		$options['limit'] = $limit;
		$options['order'] = array('Movie.added DESC');
		$options['conditions'] = array('Movie.domain_id' =>   $this->Session->read('Config.Domain.Domain.id'));
		return $this->Movie->find('all',$options);
	}
	

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->request->data['Movie']['slug'] = strtolower( Inflector::slug($this->request->data['Movie']['name'],'-') );
			$this->request->data['Movie']['domain_id'] = $this->Session->read('Config.Domain.Domain.id');
			$this->request->data['Movie']['added'] = date('Y-m-d H:i:s');
			$this->Movie->create();
			if ($this->Movie->save($this->request->data)) {
				$this->Session->setFlash(__('The movie has been saved'));
				$this->redirect(array('action' => 'admin'));
			} else {
				$this->Session->setFlash(__('The movie could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Movie->id = $id;
		if (!$this->Movie->exists()) {
			throw new NotFoundException(__('Invalid movie'));
		}
		if($this->request->is('post') || $this->request->is('put')) {
			if ($this->Movie->save($this->request->data)) {
				$this->Session->setFlash(__('The movie has been saved'));
				$this->redirect(array('action' => 'admin'));
			} else {
				$this->Session->setFlash(__('The movie could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Movie->read(null, $id);
		}
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Movie->id = $id;
		if (!$this->Movie->exists()) {
			throw new NotFoundException(__('Invalid movie'));
		}
		if ($this->Movie->delete()) {
			$this->Session->setFlash(__('Movie deleted'));
			$this->redirect(array('action' => 'admin'));
		}
		$this->Session->setFlash(__('Movie was not deleted'));
		$this->redirect(array('action' => 'admin'));
	}
}
